/* $Id: Media.java,v 1.10 2007/10/29 14:18:35 gigabass Exp $
 *
 * Copyright (c) Guido Ludwig. All rights reserved.
 * Use is subject to license terms.
 */
package de.guidoludwig.jtm.domain;

import javax.persistence.Embeddable;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;

import com.jgoodies.binding.beans.Model;


/**
 * New Media concept, unfinished
 * 
 * @author <a href="mailto:jtrade@gigabss.de">Guido Ludwig</a>
 * @version $Revision: 1.10 $
 */
@Embeddable
public class Media extends Model {

	public static final String PROPERTY_QUANTITY = "quantity";

	public static final String PROPERTY_VIDEO_SYSTEM = "videoSystem";

	public static final String PROPERTY_KBPS = "kbps";

	public static final String PROPERTY_MEDIA_TYPE = "mediaType";

	private int quantity;

	@Type(type = "de.guidoludwig.jtm.hibernate.EnumUserType", parameters = @Parameter(name = "type", value = "de.guidoludwig.jtm.domain.VideoSystem"))
	private VideoSystem videoSystem;

	private int kbps;

	@Type(type = "de.guidoludwig.jtm.hibernate.EnumUserType", parameters = @Parameter(name = "type", value = "de.guidoludwig.jtm.domain.MediaType"))
	private MediaType mediaType;

	public Media() {
		videoSystem = VideoSystem.UNKNOWN;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.guidoludwig.jtrade.domain.media.Media#getQuantity()
	 */
	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int i) {
		int old = quantity;
		quantity = i;
		firePropertyChange(PROPERTY_QUANTITY, old, quantity);
	}

	public int getKbps() {
		return kbps;
	}

	public void setKbps(int k) {
		int old = kbps;
		kbps = k;
		firePropertyChange(PROPERTY_KBPS, old, kbps);
	}

	public Media copyToNew() {
		Media newMedia = new Media();
		newMedia.setMediaType(getMediaType());
		newMedia.setQuantity(getQuantity());
		newMedia.setKbps(getKbps());
		return newMedia;
	}

	/**
	 * @return Returns the mediaType.
	 */
	public MediaType getMediaType() {
		return mediaType;
	}

	/**
	 * @param mediaType
	 *            The mediaType to set.
	 */
	public void setMediaType(MediaType mediaType) {
		Object old = this.mediaType;
		this.mediaType = mediaType;
		firePropertyChange(PROPERTY_MEDIA_TYPE, old, this.mediaType);
		if (getQuantity() == 0
				&& (this.mediaType == MediaType.CDR || this.mediaType == MediaType.DVD)) {
			setQuantity(1);
		}
	}

	@Override
	// FIXME : use formatter
	public String toString() {
		if (getMediaType() == null) {
			return "";
		}
		String value = (getQuantity() + " " + getMediaType().name());
		if (getKbps() != 0) {
			value = value + " " + getKbps() + "kbps";
		}
		if (getVideoSystem() != null && getVideoSystem() != VideoSystem.UNKNOWN) {
			value = value + " " + getVideoSystem().name();
		}
		return value;
	}

	public VideoSystem getVideoSystem() {
		return videoSystem;
	}

	public void setVideoSystem(VideoSystem videoSystem) {
		Object old = getVideoSystem();
		this.videoSystem = videoSystem;
		firePropertyChange(PROPERTY_VIDEO_SYSTEM, old, getVideoSystem());
	}
}
